<?php
/**
 * crcl/base
 *
 * Copyright 2013 CRCL - André Sabosch - info@crcl.de
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

namespace crcl\base\mvc\view;

interface IConcrete
{
  /**
   * Executes view.
   * You can fetch results with @see getOutput, @see getHeaders, @see getStatus
   * @param \crcl\base\mvc\ICommand $oCommand
   */
  public function execute(\crcl\base\mvc\ICommand $oCommand = \NULL);

  /**
   * Sets command
   *
   * @param \crcl\base\mvc\ICommand $oCommand
   */
  public function setCommand(\crcl\base\mvc\ICommand $oCommand);

  /**
   * Returns output, defaults to empty string
   *
   * @return string
   */
  public function getOutput();

  /**
   * Returns headers, defaults to empty array
   *
   * @return array
   */
  public function getHeaders();

  /**
   * Returns status, defaults to 200
   *
   * @return int
   */
  public function getStatus();
}

abstract class Base implements IConcrete
{
  public function setCommand(\crcl\base\mvc\ICommand $oCommand)
  {
    $this->oCommand = $oCommand;

    //status already set?
    $iCommandStatus = $oCommand->getStatus();
    if (!\is_null($iCommandStatus))
    {
      $this->iStatus = $iCommandStatus;
    }
    //headers already set?
    $aCommandHeaders = $oCommand->getHeaders();
    if (!\is_null($aCommandHeaders))
    {
      $this->aHeaders = $aCommandHeaders;
    }
  }



  public function getOutput()
  {
    return $this->sOutput;
  }

  public function getHeaders()
  {
    //should not occur when execute was called
    if (\is_null($this->aHeaders))
    {
      return array();
    }
    return $this->aHeaders;
  }

  public function getStatus()
  {
    return $this->iStatus;
  }

  /** PROTECTED ***********************************************************************************/

  protected $sOutput  = self::DEFAULT_OUTPUT;
  protected $aHeaders = self::DEFAULT_HEADERS;
  protected $iStatus  = self::DEFAULT_STATUS;

  protected $oCommand = \NULL;

  /** PRIVATE *************************************************************************************/

  const DEFAULT_OUTPUT  = '';
  const DEFAULT_HEADERS = \NULL;
  const DEFAULT_STATUS  = 200;
}